<?php
/**
 * Name: 论文模型.
 * User: 董坤鸿
 * Date: 2017/11/22
 * Time: 9:34
 */

namespace app\model;

use think\Model;

class Papers extends Model
{
    protected $pk = 'id';
    protected $table = 'g_papers';
    protected $insert = ['created_at'];
    protected $update = ['updated_at'];

    /**
     * 创建时间
     *
     * @param $value
     * @return false|string
     */
    protected function setCreatedAtAttr($value)
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 修改时间
     *
     * @param $value
     * @return false|string
     */
    protected function setUpdatedAtAttr($value)
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 获取模板列表
     *
     * @param  array $data
     * @param int $limit
     * @return \think\Paginator
     */
    public function lists($data, $limit = 20)
    {
        $map = [];
        if(!empty($data)){
            if (isset($data['keywords'])){
                $keywords = $data['keywords'];
                $map['name']  = ['like',"%$keywords%"];
            }
            if (isset($data['type'])){
                $map['type'] = $data['type'];
            }
        }
        $result = $this->where($map)->order('id', 'DESC')->paginate($limit);
        return $result;
    }
}